package com.huluobo.a2107mvpimdemo.demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by LC on 2023/9/23.
 */
public class MyView extends View {

    private Paint paint;//画笔

    private int progress;//绘制的进度

    private String timeString;//绘制的数字


    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    //初始化画笔
    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);//设置圆为空心
        paint.setStrokeWidth(5);//设置线的宽度
        paint.setAntiAlias(true);//设置抗锯齿,使画出的圆更丝滑

        //设置绘制文字的大小
        paint.setTextSize(100);
    }

    //重写onDraw方法,绘制的方法 Canvas 代表画布
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //给一个圆设定一个范围,左边框距离左边10px,上边框距离顶部10px,右边框距离左边510px,底边框距离顶部510px
        RectF rectF = new RectF(100, 100, 600, 600);
        //使用userCenter,在空心圆中,如果是true,如果圆不封闭,则会连接圆心.如果是false,志辉画出圆
        canvas.drawArc(rectF, 270, progress, false, paint);

        canvas.drawText(timeString, 320, 380, paint);
    }

    public void start(int time) {
        //修改圆的进度
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                progress = (int) valueAnimator.getAnimatedValue();
                invalidate();//view刷新自身的方法,更新圆的画线的进度
            }
        });
        valueAnimator.setDuration(time * 1000);
        valueAnimator.start();

        //修改文字的倒计时
        ValueAnimator valueAnimatorText = ValueAnimator.ofInt(time, 0);
        valueAnimatorText.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                timeString = String.valueOf(valueAnimatorText.getAnimatedValue());
                invalidate();
            }
        });
        valueAnimatorText.setDuration(time * 1000);
        valueAnimatorText.start();

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //4.对应需要的位置,调用接口中的方法
                onAnimEndListener.onAnimEnd();
            }
        });
    }

    //1.创建一个接口
    public interface OnAnimEndListener {
        void onAnimEnd();
    }

    //2.声明一个接口的实例
    private OnAnimEndListener onAnimEndListener;

    //3.给接口一个set的方法
    public void setOnAnimEndListener(OnAnimEndListener onAnimEndListener) {
        this.onAnimEndListener = onAnimEndListener;
    }
}
